package com.abbas.myviewtest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.abbas.myviewtest.view.listener.OnView1Listener;
import com.abbas.myviewtest.R;
import com.abbas.myviewtest.view.model.Position;

import java.util.List;

/**
 * @author abbas
 * @description: v1
 * @date 2022/3/29 19:17
 */
public class MyView1 extends ViewGroup {

    //步数
    private static int step;

    //row行 column列
    private int rowSize;
    private int columnSize;

    //单个子view的宽高
    private int childViewWidth;
    private int childViewHeight;

    //子view总数
    private int childCount;

    //当前空白部分位置
    private Position blankPosition;
    //子view集合
    private MyView2[] myView2s;

    //子view数字集合
    private int[][] childNumber;

    //监听器
    private OnView1Listener listener;

    public MyView1(Context context) {
        super(context);
        initView();
    }

    public MyView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void setListener(OnView1Listener listener) {
        this.listener = listener;
    }

    //设置行列
    public void invalidateRowAndColumn(int rowSize,int columnSize){
        this.rowSize = rowSize;
        this.columnSize = columnSize;

        //更新子view数组大小
        childNumber = new int[rowSize][columnSize];

        //移除先前绘制的view
        removeAllViews();

        //刷新数据
        initView();
        //重绘
        invalidate();
    }

    //重新排序子view数据
    public void ResortView(List<Integer> childNumber){
        int temp = -1;
        //拿到每个view设置数字
        for (int i = 0; i < childNumber.size(); i++) {
            MyView2 view2 = (MyView2) getChildAt(i);
            if (view2 != null){
                view2.setNumber(childNumber.get(i));
                int x = i % rowSize;
                if (x == 0){
                    temp++;
                }
                //打乱后重新为数组赋值
                this.childNumber[temp][x] = view2.getNumber();
            }
        }
        step = 0;
        //重新排序监听
        if (listener != null){
            listener.onViewResorted();
        }
    }

    // 初始化
    private void initView(){
        if (rowSize == 0 || columnSize == 0){
            rowSize = 4;
            columnSize = 4;
        }
        //计算子视图数量(View2数量)
        childCount = rowSize * columnSize - 1;
        //初始化子视图数组容器
        myView2s = new MyView2[childCount];
        //更新棋盘大小后刷新子view数组大小
        childNumber = new int[rowSize][columnSize];
        //初始化一个坐标
        Position position = new Position(rowSize,columnSize);
        //准备子视图
        for (int i = 0; i < childCount; i++) {
            //解析view2视图
            MyView2 view2 = (MyView2) LayoutInflater.from(getContext()).inflate(R.layout.my_view2,this,false);
            //设置定位 仅设置了总长宽
            view2.setPosition(new Position(position));
            //子view点击移动到空白区域
            view2.setOnClickListener(v -> moveToBlank(view2));

            view2.setNumber(i+1);
            //将子view添加到ViewGroup容器当中
            addView(view2);
            //添加子view到子view数组当中
            myView2s[i] = view2;
            //准备下一个位置
            position.moveToNextPosition();
            //准备完成
        }
        //最终的空白区域位置
        blankPosition = new Position(rowSize,columnSize,rowSize - 1,columnSize - 1);
    }

    //设置子view移动后的位置
    private void moveToBlank(MyView2 childView) {
        //拿到子view的位置
        final Position childPosition = childView.getPosition();
        //当前空白位置
        final Position blankPosition = this.blankPosition;

        if (childPosition.getX() == blankPosition.getX() && Math.abs(childPosition.getY() - blankPosition.getY()) == 1 ||
        childPosition.getY() == blankPosition.getY() && Math.abs(childPosition.getX() - blankPosition.getX()) == 1){

            //动画效果 左
            if (childPosition.getX() - blankPosition.getX() == 1){
                childView.animate().translationXBy(-childViewWidth);
            }else if (childPosition.getX() - blankPosition.getX() == -1){ //右
                childView.animate().translationXBy(childViewWidth);
            }else if (childPosition.getY() - blankPosition.getY() == 1){ //下
                childView.animate().translationYBy(-childViewHeight);
            }else if (childPosition.getY() - blankPosition.getY() == -1){ //上
                childView.animate().translationYBy(childViewHeight);
            }

            int a = childNumber[childPosition.getY()][childPosition.getX()];
            childNumber[childPosition.getY()][childPosition.getX()] = childNumber[blankPosition.getY()][blankPosition.getX()];
            childNumber[blankPosition.getY()][blankPosition.getX()] = a;

            //子视图在屏幕上的位置
//            childView.setX(blankPosition.getX() * childViewWidth);
//            childView.setY(blankPosition.getY() * childViewHeight);

            //给子视图设置移动后的位置
            childView.setPosition(blankPosition);
            //将空位置设置成之前的子视图位置
            this.blankPosition = childPosition;

            if (listener != null){
                listener.onChildViewMoved(getChildNumber());
            }

            //移动步数加一
            step++;

            //步数增加回调
            if (listener != null){
                listener.onStepAdded(step);
            }

            //检查是否完成游戏
            checkViewIsDone();
        }
    }

    private int[][] getChildNumber() {
        return childNumber;
    }

    private void checkViewIsDone() {
        //假如空白位置不在右下角 则不检查
        if (blankPosition.getY() != columnSize - 1 || blankPosition.getX() != rowSize - 1) return;
        for (MyView2 myView2 : myView2s) {
            int number = myView2.getNumber();
            int viewX = myView2.getPosition().getX();
            int viewY = myView2.getPosition().getY();
            if (number != viewY * rowSize + viewX + 1){
                return;
            }
        }

        if (listener != null) listener.onGameOver(step);
    }

    //自定义View第一步 测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //拿到单个view的大小
        childViewWidth = getMeasuredWidth() / rowSize;
        childViewHeight = getMeasuredHeight() / columnSize;

        //设置单个view的宽高
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            //拿到当前子view
            MyView2 childView = (MyView2) getChildAt(i);
            if (childView == null) continue;
            //设置子view数据
            LayoutParams layoutParams = childView.getLayoutParams();
            layoutParams.width = childViewWidth;
            layoutParams.height = childViewHeight;
            childView.setLayoutParams(layoutParams);
        }
    }

    //第二步 布局
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            MyView2 childView = (MyView2) getChildAt(i);
            Position position = childView.getPosition();
            final int left = position.getX() * childViewWidth;
            //此高为底
            final int top = position.getY() * childViewHeight;
            final int right = left + childViewWidth;
            //此底为高
            final int bottom = top + childViewHeight;
            childView.layout(left,top,right,bottom);
        }
    }

    //第三步 绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }


}
